package com.kernel.kotlintest

import com.github.f4b6a3.uuid.UuidCreator
import io.gitee.zhangbinhub.acp.core.common.CommonTools
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import java.io.File
import java.math.BigDecimal
import java.math.RoundingMode
import java.util.UUID

/**
 * Create by zhangbin on 2017-12-20 11:36
 */
class TestSimple {
    @Test
    fun testPath() {
        val path = CommonTools.getWebRootAbsPath() + "/files/tmp/download".replace("/", File.separator)
            .replace("\\", File.separator)
        val filterRegex: MutableList<String> = mutableListOf()
        filterRegex.addAll(
            mutableListOf(
                CommonTools.getWebRootAbsPath() + "${File.separator}files${File.separator}tmp${File.separator}.*",
                CommonTools.getWebRootAbsPath() + "${File.separator}files${File.separator}upload${File.separator}.*",
                CommonTools.getWebRootAbsPath() + "${File.separator}files${File.separator}download${File.separator}.*"
            )
        )
        println(path)
        println(pathFilter(filterRegex, path))
    }

    /**
     * 文件路径过滤
     *
     * @param filterRegex 路径
     * @param path        待匹配路径
     * @return true-允许下载 false-不允许下载
     */
    private fun pathFilter(filterRegex: List<String>, path: String): Boolean {
        path.apply {
            for (regex in filterRegex) {
                if (CommonTools.regexPattern(regex.replace("\\", "/"), this.replace("\\", "/"))) {
                    return true
                }
            }
        }
        return false
    }

    @Test
    fun testRegex() {
        val regex = "^1.*0$"
        println(CommonTools.regexPattern(regex, "nifosnai"))
        println(CommonTools.regexPattern(regex, "1nfdosa"))
        println(CommonTools.regexPattern(regex, "1nfdosa0"))
    }

    @Test
    fun moneyToCn() {
        val money = BigDecimal.valueOf(32109)
        println(CommonTools.moneyToCn(money.setScale(2, RoundingMode.HALF_UP).toDouble()))
    }

    @Test
    fun numberToCn() {
        val number1 = BigDecimal.valueOf(32109.21)
        val number2 = BigDecimal.valueOf(50000.00)
        val number3 = BigDecimal.valueOf(748.59)
        val number4 = BigDecimal.valueOf(32109)
        val number5 = BigDecimal.valueOf(5109.1)
        val number6 = BigDecimal.valueOf(5000.01)
        println("$number1 ----> ${CommonTools.numberToCn(number1.toDouble(), 2)}")
        println("$number2 ----> ${CommonTools.numberToCn(number2.toDouble(), 2)}")
        println("$number3 ----> ${CommonTools.numberToCn(number3.toDouble(), 2)}")
        println("$number4 ----> ${CommonTools.numberToCn(number4.toDouble(), 2)}")
        println("$number5 ----> ${CommonTools.numberToCn(number5.toDouble(), 5)}")
        println("$number6 ----> ${CommonTools.numberToCn(number6.toDouble(), 2)}")
    }

    @Test
    fun testDelete() {
        CommonTools.doDeleteFile(File("D:\\test\\file\\新建文本文档.bat"), true, 5000)
        runBlocking {
            delay(10000)
        }
    }

    @Test
    fun testByte() {
        println('s'.code)
        println('s'.code.toByte())
    }

    @Test
    fun testString() {
        val src = "你好hello world"
        println(CommonTools.strFillIn(src, 15, 0, '赞'))
        println(CommonTools.strFillIn(src, 15, 1, '赞'))
        println(CommonTools.strFillIn(src, 15, 0, 'H'))
        println(CommonTools.strFillIn(src, 15, 1, 'H'))
    }

    @Test
    fun testUuid(){
        val begin = System.currentTimeMillis()
        for (i in 1..100000) {
            UUID.randomUUID().toString()
        }
        println("JDK UUID: ${System.currentTimeMillis()-begin} ms")

        val beginCT = System.currentTimeMillis()
        for (i in 1..100000) {
            CommonTools.getUuid()
//            UuidCreator.getTimeOrderedEpochPlus1().toString()
        }
        println("CommonTools UUID: ${System.currentTimeMillis()-beginCT} ms")
    }
}
